<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2020/1/17
 * Time: 15:07
 */

namespace app\index\controller;
use app\common\event\GoodsEvent;
use app\common\model\BaseGoods;
use app\common\model\Brand;
use app\common\model\Category;
use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsAttr;
use app\common\model\GoodsAttribute;
use app\common\model\ShopCategory;
use app\common\model\Shop as ShopModel;
use app\common\model\Discount as DiscountModel;
use app\common\model\Spec;
use app\common\model\SpecGoodsPrice;
use app\common\model\SpecItem;

class Goods extends Base
{

    public function goodsList(){

        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;

        $filter_param = array(); // 筛选数组

        $cat_id = input('get.cat_id/d'); // 当前分类id

        $currentGoodsCate = Category::getOne(['id'=>$cat_id]);

        $brand_id = input('get.brand_id',0);
        $spec = input('get.spec',0); // 规格
        $attr = input('get.attr',''); // 属性
        $sort = input('get.sort','goods_id'); // 排序
        $sort_asc = input('get.sort_asc','desc'); // 排序
        $search_key = input('search_key','');

        $price = input('get.price',''); // 价钱
        $start_price = trim(input('post.start_price','0')); // 输入框价钱
        $end_price = trim(input('post.end_price','0')); // 输入框价钱
        if($start_price && $end_price) $price = $start_price.'-'.$end_price; // 如果输入框有价钱 则使用输入框的价钱


        $filter_param['cat_id'] = $cat_id; //加入筛选条件中
        $brand_id  && ($filter_param['brand_id'] = $brand_id); //加入筛选条件中
        $spec  && ($filter_param['spec'] = $spec); //加入筛选条件中
        $attr  && ($filter_param['attr'] = $attr); //加入筛选条件中
        $price  && ($filter_param['price'] = $price); //加入筛选条件中
        $page && ($filter_param['page'] = $page);

        $where[] = ['is_on_sale','=',1];
        $cat_id_arr = [];
        if(!empty($cat_id)){
            $category = Category::getTree($cat_id);
            $cat_id_arr = array_column($category,'id');
            if(empty($son)){
                $cat_id_arr[] = $cat_id;
            }
            $where[] = ['cat_id','in',implode(',', $cat_id_arr)];
        }

        if(!empty($search_key)){
            //$where = [['figure_nu','like',"%$search_key%"]];
            //$filter_goods_id =  GoodsModel::where($where)->column('goods_id');
            $where[] = ['figure_nu','like',"%$search_key%"];
        }

        $filter_goods_id = GoodsModel::where($where)->column("goods_id");
        //print_r($filter_goods_id);die();

        // 过滤筛选的结果集里面找商品
        if($brand_id || $price)// 品牌或者价格
        {
            $goods_id_1 = GoodsEvent::getGoodsIdByBrandPrice($brand_id,$price); // 根据 品牌 或者 价格范围 查找所有商品id
            $filter_goods_id = array_intersect($filter_goods_id,$goods_id_1); // 获取多个筛选条件的结果 的交集
        }

        if($spec)// 规格
        {
            $goods_id_2 = GoodsEvent::getGoodsIdBySpec($spec); // 根据 规格 查找当所有商品id
            $filter_goods_id = array_intersect($filter_goods_id,$goods_id_2); // 获取多个筛选条件的结果 的交集
        }

        if($attr)// 属性
        {
            $goods_id_3 = GoodsEvent::getGoodsIdByAttr($attr); // 根据 规格 查找当所有商品id
            $filter_goods_id = array_intersect($filter_goods_id,$goods_id_3); // 获取多个筛选条件的结果 的交集
        }
        
        // 获取显示的筛选菜单
        $filter_menu  = GoodsEvent::get_filter_menu($filter_param,'goodsList');

        //筛选的价格期间
        $filter_price = GoodsEvent::get_filter_price($filter_goods_id,$filter_param,'goodsList');

        //获取指定分类下的筛选品牌
        $filter_brand = GoodsEvent::get_filter_brand($filter_goods_id,$filter_param,'goodsList');

        //获取指定分类下的筛选规格
        $filter_spec  = GoodsEvent::get_filter_spec($filter_goods_id,$filter_param,'goodsList',1);
        //获取指定分类下的筛选属性
        $filter_attr  = GoodsEvent::get_filter_attr($filter_goods_id,$filter_param,'goodsList',1);
        //print_r( $filter_goods_id);die();

        //开始查找商品
        $count = count($filter_goods_id);

        $goods_list = [];
        if($count > 0){
            $where[] = ["goods_id","in", implode(',', $filter_goods_id)];
            $condition = [
                'with'  => ['getShop'],
                'where' => $where,
                'order' => "$sort $sort_asc",
                'field' => 'goods_id,shop_id,goods_name,shop_price,original_img,goods_type,sales_sum,comment_count',
                'paginate' => [
                    'page' => $page,
                    'query' => $filter_param,
                    'list_rows'=>20,
                ],
            ];

            $goods_list = goodsModel::getPageAll($condition);
        }


        //推荐商品
        $recommendGoods = GoodsModel::getRecommendGoods(['is_on_sale'=>1,'is_recommend'=>1],4);

        //$navigate_cat = navigate_goods($id); // 面包屑导航
        $this->assign('goods_list',$goods_list);
        $this->assign('recommendGoods',$recommendGoods);
        //$this->assign('navigate_cat',$navigate_cat);
        //$this->assign('goods_category',$goods_category);
        //$this->assign('goods_images',$goods_images);  // 相册图片
        $this->assign('filter_menu',$filter_menu);  // 帅选菜单
        $this->assign('filter_spec',$filter_spec);  // 帅选规格
        $this->assign('filter_attr',$filter_attr);  // 帅选属性
        $this->assign('filter_brand',$filter_brand);  // 列表页帅选属性 - 商品品牌
        $this->assign('filter_price',$filter_price);// 帅选的价格期间
        $this->assign('currentGoodsCate',$currentGoodsCate);
        //$this->assign('cateArr',$cateArr);
        $this->assign('filter_param',$filter_param); // 帅选条件
        $this->assign('cat_id',$cat_id);
        return $this->fetch();
    }

    public function goodsDetail(){
        $goods_id = $this->request->param('goods_id');
        $goods = GoodsModel::findModel($goods_id,[]);
        if(empty($goods) || ($goods['is_on_sale'] == 0)){
            $this->failed('该商品已经下架',url('index/index'));
        }
        if(!empty($goods['photo_album'])){
            $goods['photo_album'] = unserialize($goods['photo_album']);
        }
        $this->assign('goods',$goods);
        //商品库信息
        $base_goods = BaseGoods::getOne(['id'=>$goods['base_goods_id']]);
        $this->assign('base_goods',$base_goods);
        //店铺信息
        $shop_id = $goods['shop_id'];
        $this->assign('shop_id',$shop_id);
        $shop = ShopModel::findModel($shop_id,[]);
        $this->assign('shop',$shop);

        //商品属性
        $goodsAttribute = GoodsAttribute::where(['type_id'=>$goods['goods_type']])->column('attr_name','attr_id');
        $goodsAttr = GoodsAttr::getAll(['goods_id'=>$goods_id],'attr_id,attr_value');
        $this->assign(['goodsAttribute'=>$goodsAttribute,'goodsAttr'=>$goodsAttr]);

        //商品规格
        $filter_spec = GoodsModel::get_spec($goods_id);
        //print_r($filter_spec);die();
        $this->assign('filter_spec',$filter_spec);
        //商品品牌
        if($goods['brand_id']){
            $goods['brand_name'] = Brand::where("id",$goods['brand_id'])->value('name');
        }

        //是否折扣商品
        $time = time();
        $where[] = ['goods_id','=',$goods_id];
        $where[] = ['start_time','<',$time];
        $where[] = ['end_time','>',$time];
        $discount = DiscountModel::getOne($where);
        $this->assign('discount',$discount);

        if(!empty($discount) && $goods['exchange_integral'] > 0){
            $goods['price'] = ($goods['shop_price']*$discount['rate']/100).'+'.$goods['exchange_integral'].'积分';
        }else if(!empty($discount)){
            $goods['price'] = $goods['shop_price']*$discount['rate']/100;
        }else if($goods['exchange_integral'] > 0){
            $goods['price'] = $goods['shop_price'].'+'.$goods['exchange_integral'].'积分';
        }

        //取出店铺分类
        $shopCategory = ShopCategory::getAllCategory($shop_id);
        $this->assign('shopCategory',$shopCategory);
        //推荐商品
        $recommendGoods = GoodsModel::getRecommendGoods(['shop_id'=>$shop_id,'is_on_sale'=>1,'is_recommend'=>1],12);
        //新品上架
        $newGoods = GoodsModel::getRecommendGoods(['shop_id'=>$shop_id,'is_on_sale'=>1,'is_new'=>1],3);
        $this->assign('goods_id',$goods_id);
        $this->assign('newsGoods',$newGoods);
        $this->assign('recommendGoods',$recommendGoods);
        return $this->fetch();
    }

}